home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Menús / FontMenu / FontMenu.cs next >
Encoding:
Text File  |  2002-06-19  |  2.1 KB  |  66 lines

  1. //---------------------------------------
  2. // FontMenu.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class FontMenu: Form
  9. {
  10.      const int iPointSize = 24;
  11.      string    strFacename;
  12.  
  13.      public static void Main()
  14.      {
  15.           Application.Run(new FontMenu());
  16.      }
  17.      public FontMenu()
  18.      {
  19.           Text = "Men· Fuentes";
  20.  
  21.           strFacename = Font.Name;
  22.  
  23.           Menu = new MainMenu();
  24.  
  25.           MenuItem mi = new MenuItem("&Nombres de fuente");
  26.           mi.Popup += new EventHandler(MenuFacenameOnPopup);
  27.           mi.MenuItems.Add(" ");   // Necesario para la llamada emergente
  28.           Menu.MenuItems.Add(mi);
  29.      }
  30.      void MenuFacenameOnPopup(object obj, EventArgs ea)
  31.      {
  32.           MenuItem     miFacename = (MenuItem)obj;
  33.           FontFamily[] aff        = FontFamily.Families;
  34.           EventHandler ehClick    = new EventHandler(MenuFacenameOnClick);
  35.           MenuItem[]   ami        = new MenuItem[aff.Length];
  36.  
  37.           for (int i = 0; i < aff.Length; i++)
  38.           {
  39.                ami[i] = new MenuItem(aff[i].Name);
  40.                ami[i].Click += ehClick;
  41.  
  42.                if (aff[i].Name == strFacename)
  43.                     ami[i].Checked = true;
  44.           }
  45.           miFacename.MenuItems.Clear();
  46.           miFacename.MenuItems.AddRange(ami);
  47.      }
  48.      void MenuFacenameOnClick(object obj, EventArgs ea)
  49.      {    
  50.           MenuItem mi = (MenuItem)obj;
  51.           strFacename = mi.Text;
  52.           Invalidate();
  53.      }
  54.      protected override void OnPaint(PaintEventArgs pea)
  55.      {
  56.           Graphics grfx = pea.Graphics;
  57.           Font     font = new Font(strFacename, iPointSize);
  58.  
  59.           StringFormat strfmt  = new StringFormat();
  60.           strfmt.Alignment     = StringAlignment.Center;
  61.           strfmt.LineAlignment = StringAlignment.Center;
  62.  
  63.           grfx.DrawString("Texto de ejemplo", font, new SolidBrush(ForeColor), 
  64.                           ClientRectangle, strfmt);
  65.      }                        
  66. }